<template>
  <article class="m-rich-text" v-html="safeHtml" />
</template>

<script setup>
import { computed } from "vue";
import DOMPurify from "dompurify";

const props = defineProps({
  html: { type: String, default: "" },
});

/* 白名单：只允许常用标签 + 图片 + 视频 + 表格 */
const safeHtml = computed(() =>
  DOMPurify.sanitize(props.html, {
    ADD_TAGS: ["video"],
    ADD_ATTR: ["poster", "controls"],
    FORBID_ATTR: ["style", "class"], // 强制剃掉所有行内样式
  })
);
</script>

<style>
/* ========== 移动端专用重置 ========== */
.m-rich-text {
  font-size: 16px;
  line-height: 1.75;
  color: #1a1a1a;
  overflow-wrap: break-word; /* 长单词截断 */
  word-break: break-all;
}

/* 段落 */
.m-rich-text p {
  margin: 0 0 1em;
}

/* 标题 */
.m-rich-text h1,
.m-rich-text h2,
.m-rich-text h3,
.m-rich-text h4,
.m-rich-text h5,
.m-rich-text h6 {
  line-height: 1.4;
  margin: 1.2em 0 0.6em;
  font-weight: 600;
}
.m-rich-text h1 {
  font-size: 22px;
}
.m-rich-text h2 {
  font-size: 20px;
}
.m-rich-text h3 {
  font-size: 18px;
}

/* 图片 & 视频 */
.m-rich-text img,
.m-rich-text video {
  max-width: 100% !important;
  height: auto !important;
  display: block;
}

/* 表格 */
.m-rich-text table {
  width: 100% !important;
  border-collapse: collapse;
  table-layout: fixed; /* 防止列宽被内容撑开 */
  margin: 0.6em 0;
}
.m-rich-text table th,
.m-rich-text table td {
  padding: 8px 6px;
  border: 1px solid #e5e5e5;
  font-size: 14px;
}
.m-rich-text table th {
  background: #f7f7f7;
  font-weight: 600;
}

/* 列表 */
.m-rich-text ul,
.m-rich-text ol {
  padding-left: 1.2em;
  margin: 0 0 1em;
}
.m-rich-text li {
  margin: 0.3em 0;
}

/* 引用 */
.m-rich-text blockquote {
  margin: 1em 0;
  padding: 0.6em 1em;
  background: #f5f5f5;
  border-left: 4px solid #ccc;
  color: #555;
}

/* 链接 */
.m-rich-text a {
  color: #576b95;
  text-decoration: none;
  word-break: break-all;
}
.m-rich-text a:active {
  opacity: 0.7;
}

/* 代码 */
.m-rich-text code {
  padding: 2px 4px;
  background: #f1f1f1;
  border-radius: 3px;
  font-size: 0.9em;
}
.m-rich-text pre {
  overflow-x: auto; /* 代码块横向滚动 */
  padding: 0.8em;
  background: #f8f8f8;
  border-radius: 4px;
  font-size: 14px;
  line-height: 1.4;
}
</style>
